/**
 * 作者: wgx
 * 日期: 2025-06-17 10:52:13
 * 备注: 用户的秘书经营技能表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserSecretaryBusiness;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserSecretaryBusinessModel extends BaseModel implements Model {
    public static final String LISTKEY = "unique_id.skill_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // 数据库ID
    protected String uniqueId = ""; // userid_秘书id
    protected int skillId; // 技能id (potential_skill表的ID)
    protected int secretaryId; // 秘书ID
    protected int value; // 加成 (万分比)
    protected int levelCount; // 升级次数

    public void reset() {
    }

    protected String getKey() {
        return "UserSecretaryBusiness." + uniqueId + "." + skillId;
    }

    protected String getListKey() {
        return "UserSecretaryBusiness." + uniqueId;
    }

    protected String getListItemKey() {
        return skillId + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_secretary_business"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, 0, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // 数据库ID
        obj.put("uniqueId", uniqueId); // userid_秘书id
        obj.put("skillId", skillId); // 技能id (potential_skill表的ID)
        obj.put("secretaryId", secretaryId); // 秘书ID
        obj.put("value", value); // 加成 (万分比)
        obj.put("levelCount", levelCount); // 升级次数
        return obj;
    }

    public PBUserSecretaryBusiness.Builder takeInitBuilder() {
        PBUserSecretaryBusiness.Builder sb = PBUserSecretaryBusiness.newBuilder();
        if (skillId != 0) {
            sb.setSkillId(skillId);
        }
        if (value != 0) {
            sb.setValue(value);
        }
        if (levelCount != 0) {
            sb.setLevelCount(levelCount);
        }
        return sb;
    }

    public PBUserSecretaryBusiness.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserSecretaryBusiness.Builder sb = PBUserSecretaryBusiness.newBuilder();
        if (isFieldUpdate("skillId")) {
            sb.setSkillId(skillId);
        }
        if (isFieldUpdate("value")) {
            sb.setValue(value);
        }
        if (isFieldUpdate("levelCount")) {
            sb.setLevelCount(levelCount);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public String getUniqueId() {
        return uniqueId;
    }

    public void putUniqueId(String uniqueId) {
        if (!this.uniqueId.equals(uniqueId)) {
            this.uniqueId = uniqueId;
            updateRedisHashField(getKey(), "uniqueId", this.uniqueId);
        }
    }

    public int getSkillId() {
        return skillId;
    }

    public void putSkillId(int skillId) {
        if (this.skillId != skillId) {
            this.skillId = skillId;
            updateRedisHashField(getKey(), "skillId", this.skillId);
        }
    }

    public void addSkillId(int value) {
        this.skillId += value;
        updateRedisHashField(getKey(), "skillId", this.skillId);
    }

    public int getSecretaryId() {
        return secretaryId;
    }

    public void putSecretaryId(int secretaryId) {
        if (this.secretaryId != secretaryId) {
            this.secretaryId = secretaryId;
            updateRedisHashField(getKey(), "secretaryId", this.secretaryId);
        }
    }

    public void addSecretaryId(int value) {
        this.secretaryId += value;
        updateRedisHashField(getKey(), "secretaryId", this.secretaryId);
    }

    public int getValue() {
        return value;
    }

    public void putValue(int value) {
        if (this.value != value) {
            this.value = value;
            updateRedisHashField(getKey(), "value", this.value);
        }
    }

    public void addValue(int value) {
        this.value += value;
        updateRedisHashField(getKey(), "value", this.value);
    }

    public int getLevelCount() {
        return levelCount;
    }

    public void putLevelCount(int levelCount) {
        if (this.levelCount != levelCount) {
            this.levelCount = levelCount;
            updateRedisHashField(getKey(), "levelCount", this.levelCount);
        }
    }

    public void addLevelCount(int value) {
        this.levelCount += value;
        updateRedisHashField(getKey(), "levelCount", this.levelCount);
    }

}